#include "menu_more_set.h"
#include "menu_frame.h"
#include "language.h"
#include <stdio.h>
#include <string.h>

/* 更多设置 */
ST_MENU_LIST gastMoreSetMenuTable[] = {
    MMI_MENU_ITEM_BIND(TEXT_UPGRADE, NULL, NULL, NULL, MENU_OnCommonFunction, NULL),
    MMI_MENU_ITEM_BIND(TEXT_ABOUT, NULL, NULL, NULL, MENU_OnCommonFunction, NULL),
};

/* 更多设置菜单显示效果: 右侧弹出菜单效果 */
static void MENU_ShowMoreMenu(ST_MENU_SHOW *ptShowInfo)
{
    uint8_t showNum = 3;
    uint8_t showsubNum = 3;
    uint8_t tmpselect;

    ST_MENU_SHOW tParentMenuShowInfo;

    if (MMI_MenuQueryParentMenu(&tParentMenuShowInfo, 1) != 0)
    {
        return;
    }

    MMI_MenuLimitShowListNumber(&tParentMenuShowInfo, &showNum);
    MMI_MenuLimitShowListNumber(ptShowInfo, &showsubNum);

    printf("\e[0;30;46m ------------- %s ------------- \e[0m\n", get_text(tParentMenuShowInfo.uMenuDesc.textId));

    for (int i = 0; i < showNum; i++)
    {
        tmpselect = i + tParentMenuShowInfo.showBaseItem;

        if (tmpselect == tParentMenuShowInfo.selectItem)
        {
            printf("\e[0;30;47m %d. %-16s\e[0m |", tmpselect + 1, get_text(tParentMenuShowInfo.uItemsListDesc[tmpselect].textId));
        }
        else
        {
            printf("\e[7;30;47m %d. %-16s\e[0m |", tmpselect + 1, get_text(tParentMenuShowInfo.uItemsListDesc[tmpselect].textId));
        }

        if (i < showsubNum)
        {
            tmpselect = i + ptShowInfo->showBaseItem;

            if (tmpselect == ptShowInfo->selectItem)
            {
                printf(" \e[0;30;47m %-14s\e[0m", get_text(ptShowInfo->uItemsListDesc[tmpselect].textId));
            }
            else
            {
                printf(" \e[7;30;47m %-14s\e[0m", get_text(ptShowInfo->uItemsListDesc[tmpselect].textId));
            }
        }

        printf("\n");
    }
}

void MENU_MoreSetEnter(const ST_MENU_ITEM_INFO *pItemInfo)
{
    MMI_MenuBind(gastMoreSetMenuTable, MMI_GET_MENU_NUM(gastMoreSetMenuTable), MENU_ShowMoreMenu);
}

void MENU_MoreSetLoad(const ST_MENU_ITEM_INFO *pItemInfo)
{
}

void MENU_MoreSetExit(const ST_MENU_ITEM_INFO *pItemInfo)
{
}

void MENU_MoreSetTask(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
#if 0
    int cmd;

    printf("%s(0-%s; 1-%s%s; 2-%s; 3-%s; 4-%s): ",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_RETURN), get_text(TEXT_RETURN), get_text(TEXT_MAIN_MENU),
           get_text(TEXT_ENTER), get_text(TEXT_NEXT), get_text(TEXT_PREVIOUS));
    scanf(" %d", &cmd); // 空格作用是忽略上次的回车

    switch (cmd)
    {
    case 0:
        MMI_MenuExit(false);
        break;
    case 1:
        MMI_MenuReset();
        break;
    case 2:
        MMI_MenuEnter();
        break;
    case 3:
        MMI_MenuSelectNext(true);
        break;
    case 4:
        MMI_MenuSelectPrev(true);
        break;

    default:
        break;
    }
#else
    if (!pKeyInfo)
        return;

    switch (pKeyInfo->code)
    {
    case EN_USER_KEY_SOFT4:
        MMI_MenuExit(false);
        break;
    case EN_USER_KEY_OK:
        MMI_MenuEnter();
        break;
    case EN_USER_KEY_RIGHT:
        MMI_MenuSelectNext(true);
        break;
    case EN_USER_KEY_LEFT:
        MMI_MenuSelectPrev(true);
        break;

    case EN_USER_KEY_8:
        MMI_MenuReset();
        break;
    default:
        break;
    }
    printf("%s(SOFT4-%s; 8-%s%s; OK-%s; RIGHT-%s; LEFT-%s):\n",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_RETURN), get_text(TEXT_RETURN), get_text(TEXT_MAIN_MENU),
           get_text(TEXT_ENTER), get_text(TEXT_NEXT), get_text(TEXT_PREVIOUS));
#endif
}
